package com.sojson.controller.po.param.verify;

import com.sojson.config.exception.TransErrorCode;
import com.sojson.constant.Constant;
import com.sojson.entity.po.Member;
import com.sojson.enums.core.ERoleType;
import com.sojson.result.Result;
import com.sojson.util.DateUtil;
import com.sojson.util.MatcheUtil;
import com.sojson.util.StringUtil;
import com.sojson.util.token.TokenUtil;
import com.sojson.util.token.TokenVo;
import com.sojson.util.verifycode.VerifyCodeUtil;

import io.jsonwebtoken.Claims;

/**
 * 认证Controller参数验证
 * 
 * @author liu
 * @date 2021-03-16
 */
public class AuthcParamVerify {

    /**
     * 帐号密码注册
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    public static Result<?> pwdRegister(TokenVo token) throws Exception {
        String verifyCodeValue = token.getVerifyCodeValue();
        if (StringUtil.isBlank(verifyCodeValue)) {
            return Result.fail("验证码不能为空！");
        }
        Member member = token.getMember();
        if (StringUtil.isBlankObject(member)) {
            return Result.fail("member参数不能为空！");
        }
        if (StringUtil.isBlankObject(member.getNickName())) {
            member.setNickName(DateUtil.nowLong() + "");
        }
        // if (StringUtil.isBlankObject(member.getHeadImg())) {
        // member.setHeadImg(ConstantByMap.DEFAULT_HEAD);
        // }
        String username = member.getUsername();

        if (StringUtil.isBlank(username)) {
            return Result.fail("帐号不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            member.setPwd(Constant.USER_PASSWORD_DEFAULT);
        }
        Integer roleId = token.getRoleId();
        if (StringUtil.isBlank(roleId)) {
            return Result.fail("角色ID不能为空！");
        }
        boolean isSucc = false;
        ERoleType[] values = ERoleType.values();
        for (ERoleType eRoleType : values) {
            if (eRoleType.getCode() == roleId) {
                isSucc = true;
                break;
            }
        }
        if (!isSucc) {
            return Result.fail("角色不存在！");
        }
        if (roleId == ERoleType.ADMIN.getCode()) {
            return Result.fail("不能添加超级管理员！");
        } else if (roleId == ERoleType.LABOR.getCode()) {
            if (StringUtil.isBlank(member.getLaborCard())) {
                return Result.fail("劳务派遣证不能为空！");
            }
        } else if (roleId == ERoleType.MERCHANT.getCode()) {
            if (StringUtil.isBlank(member.getFoodCard())) {
                return Result.fail("食品经营许可证不能为空！");
            }
            if (StringUtil.isBlank(member.getTelecomCard())) {
                return Result.fail("电信增值许可证不能为空！");
            }
            if (StringUtil.isBlank(member.getMerchantTypes())) {
                return Result.fail("商户类型不能为空！");
            }
            if (StringUtil.isBlank(member.getCity())) {
                return Result.fail("市不能为空！");
            }
            if (StringUtil.isBlank(member.getArea())) {
                return Result.fail("区不能为空！");
            }
            if (StringUtil.isBlank(member.getAddress())) {
                return Result.fail("详细地址不能为空！");
            }
            Float targetX = member.getTargetX();
            if (StringUtil.isBlank(targetX)) {
                return Result.fail("经度不能为空！");
            }
            Float targetY = member.getTargetY();
            if (StringUtil.isBlank(targetY)) {
                return Result.fail("维度不能为空！");
            }
            if (targetX > 180 || targetX <= -180 || targetY > 90 || targetY < -90) {
                return Result.fail("经纬度值错误！");
            }
        } else if (roleId == ERoleType.USER.getCode()) {
            return Result.fail("普通用户请通过小程序添加！");
        }
        Claims claims = TokenUtil.getClaims(token.getToken());
        if (StringUtil.isBlankObject(claims)) {
            return Result.fail("Token不能为空！");
        }
        String id = claims.getId();
        if (!VerifyCodeUtil.verifyAndClearCode(
            new StringBuilder(id).append(VerifyCodeUtil.IMG).append(token.getVerifyCodePrefix()).toString(),
            verifyCodeValue)) {
            return Result.fail(TransErrorCode.ERR_CODES.getMsg());
        }

        return Result.success();
    }

    /**
     * 密码登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    public static Result<?> pwdLogin(TokenVo token) throws Exception {
        String verifyCodeValue = token.getVerifyCodeValue();
        if (StringUtil.isBlank(verifyCodeValue)) {
            return Result.fail("验证码不能为空！");
        }
        Member member = token.getMember();
        if (StringUtil.isBlankObject(member)) {
            return Result.fail("member参数不能为空！");
        }
        String username = member.getUsername();

        if (StringUtil.isBlank(username)) {
            return Result.fail("帐号不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            return Result.fail("密码不能为空！");
        }
        Claims claims = TokenUtil.getClaims(token.getToken());
        if (StringUtil.isBlankObject(claims)) {
            return Result.fail("Token不能为空！");
        }
        String id = claims.getId();
        if (!VerifyCodeUtil.verifyAndClearCode(
            new StringBuilder(id).append(VerifyCodeUtil.IMG).append(token.getVerifyCodePrefix()).toString(),
            verifyCodeValue)) {
            return Result.fail(TransErrorCode.ERR_CODES.getMsg());
        }

        return Result.success();
    }

    /**
     * 密码登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    public static Result<?> pwdLoginApp(TokenVo token) throws Exception {
        Member member = token.getMember();
        if (StringUtil.isBlankObject(member)) {
            return Result.fail("member参数不能为空！");
        }
        String username = member.getUsername();

        if (StringUtil.isBlank(username)) {
            return Result.fail("帐号不能为空！");
        }
        if (StringUtil.isBlank(member.getPwd())) {
            return Result.fail("密码不能为空！");
        }

        return Result.success();
    }

    /**
     * 微信小程序登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    public static Result<?> wxXcxLogin(TokenVo token) throws Exception {
        Member member = token.getMember();
        if (StringUtil.isBlankObject(member)) {
            return Result.fail("member参数不能为空！");
        }
        if (StringUtil.isBlankObject(member.getNickName())) {
            return Result.fail("昵称不能为空！");
        }
        if (StringUtil.isBlankObject(member.getHeadImg())) {
            return Result.fail("头像不能为空！");
        }
        if (StringUtil.isBlankObject(member) || StringUtil.isBlank(member.getUsername())) {
            return Result.fail("js_code不能为空！");
        }

        return Result.success();
    }

    /**
     * 短信登录
     * 
     * @param token 认证用户Bean
     * @return
     * @throws Exception 
     */
    public static Result<?> smsLogin(TokenVo token) throws Exception {
        String verifyCodeValue = token.getVerifyCodeValue();
        if (StringUtil.isBlank(verifyCodeValue)) {
            return Result.fail("验证码不能为空！");
        }
        Member member = token.getMember();
        if (StringUtil.isBlankObject(member)) {
            return Result.fail("member参数不能为空！");
        }
        String mobile = member.getMobile();
        if (StringUtil.isBlank(mobile)) {
            return Result.fail("手机号不能为空！");
        }
        if (!MatcheUtil.isMobile(mobile)) {
            return Result.fail("手机号格式错误！");
        }
        Claims claims = TokenUtil.getClaims();
        if (StringUtil.isBlankObject(claims)) {
            return Result.fail("Token不能为空！");
        }
        String id = claims.getId();
        if (!VerifyCodeUtil.verifyAndClearCode(
            new StringBuilder(id).append(VerifyCodeUtil.SMS).append(token.getVerifyCodePrefix()).toString(),
            verifyCodeValue)) {
            return Result.fail(TransErrorCode.ERR_CODES.getMsg());
        }

        return Result.success();
    }

}